﻿Option Explicit On
Option Strict On

Imports System.Collections.Generic


Public Class cStringBuilder
    Implements IDisposable
    Implements TreeCreeper01Interfaces.IResultsTable

    Private pCurrentRowIndex As Integer
    Private pCurrentRowDataDictionary As SortedDictionary(Of Long, String)
    Private pStringBuilder As System.Text.StringBuilder


    Public Sub New(ByVal StringBuilder As System.Text.StringBuilder)
        pCurrentRowIndex = 1
        pCurrentRowDataDictionary = New SortedDictionary(Of Long, String)
        pStringBuilder = StringBuilder
    End Sub


    Sub InsertString(ByVal ColumnIndex As Long, ByVal Value As String) Implements TreeCreeper01Interfaces.IResultsTable.InsertString
        pCurrentRowDataDictionary.Add(ColumnIndex, Value)
    End Sub

    Sub InsertNumber(ByVal ColumnIndex As Long, ByVal Number As Double, Optional ByVal FormattingString As String = "") Implements TreeCreeper01Interfaces.IResultsTable.InsertNumber

        Dim NumberAsString As String

        NumberAsString = Format(Number, FormattingString)

        pCurrentRowDataDictionary.Add(ColumnIndex, NumberAsString)

    End Sub

    Public Property Name() As String Implements TreeCreeper01Interfaces.IResultsTable.Name
        Get
            ' does nothing in filewriter
            Return "NO NAME IN FILE WRITER MODE"
        End Get
        Set(ByVal value As String)
            ' does nothing in filewriter
        End Set
    End Property

    Public ReadOnly Property CurrentRowIndex() As Integer Implements TreeCreeper01Interfaces.IResultsTable.CurrentRowIndex
        Get
            Return pCurrentRowIndex
        End Get
        'Set(ByVal value As Integer)
        '    pCurrentRowIndex = value
        'End Set
    End Property

    Public Sub InsertComment(ByVal ColumnIndex As Long, ByVal Comment As String) Implements TreeCreeper01Interfaces.IResultsTable.InsertComment

    End Sub

    Public Sub IncrementCurrentRowIndex() Implements TreeCreeper01Interfaces.IResultsTable.IncrementCurrentRowIndex
        WriteOutCurrentRow()
        pCurrentRowIndex = pCurrentRowIndex + 1
    End Sub

    Private Sub WriteOutCurrentRow()

        Dim CurrentKey As Long
        Dim PreviousKey As Long
        Dim CurrentValue As String
        Dim RowData As String

        RowData = ""

        PreviousKey = 1
        For Each CurrentKey In pCurrentRowDataDictionary.Keys()
            CurrentValue = pCurrentRowDataDictionary.Item(CurrentKey)
            ' add tabs as needed
            While CurrentKey - PreviousKey > 0
                pStringBuilder.Append(vbTab)
                PreviousKey = PreviousKey + 1
            End While
            ' write out the single value, pad to 16 chars if necessary to align things nicely
            CurrentValue = CurrentValue.PadRight(16)
            pStringBuilder.Append(CurrentValue)
        Next

        ' change+restart the row
        pStringBuilder.Append(vbNewLine)

        pCurrentRowDataDictionary = New SortedDictionary(Of Long, String)

    End Sub


    Public Sub Dispose() Implements System.IDisposable.Dispose, TreeCreeper01Interfaces.IResultsTable.Dispose
        ' nothing
    End Sub


End Class
